package com.demo.forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * 统一结果返回
 */
public class Result<T>{
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private long code;
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;
    @JsonInclude()
    private T data;

    /**
     * 构造方法
     * @param code
     * @param message
     * @param data
     */

    public Result(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Result(long code, String message) {
        this(code,message,null);
    }

    /**
     * 成功
     * @return
     */
    public static Result success(){
        return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.message);

    }

    public static <T>Result<T> success(T data){
        return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.message,data);

    }
    public static <T>Result<T> success(String message,T data){
        return new Result(ResultCode.SUCCESS.getCode(),message,data);

    }

    /**
     * 失败
     */


    public static Result failed(){
        return new Result(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessag());
    }

    public static Result failed(String message){
        return new Result(ResultCode.FAILED.getCode(), message);
    }
    public static Result failed(ResultCode resultCode){

        return new Result(resultCode.getCode(),resultCode.getMessag());
    }

    public static <T>Result<T> failed(T data){

        return new Result(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessag(),data);
    }


    /**
     * get , set 方法
     * @return
     */
    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
